// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <vector>

#include <ktest/ktest.h>

#include <nebula/array/builder_binary.h>
#include <nebula/array/builder_primitive.h>
#include <nebula/types/scalar.h>
#include <nebula/core/table.h>
#include <nebula/testing/ktest_util.h>
#include <nebula/util/rows_to_batches.h>

namespace nebula::util {

const auto kTestSchema = schema(
    {field("field_1", int64()), field("field_2", int64()), field("field_3", int64())});

auto IntConvertor = [](ArrayBuilder& array_builder, int value) {
  return static_cast<Int64Builder&>(array_builder).append(value);
};

bool CompareJson(const nebula::Table& arrow_table, const std::string& json,
                 const std::string& field_name) {
  const auto col = arrow_table.get_column_by_name(field_name);
  return nebula::assert_chunked_array_from_json(col->type(), {json})->equals(col);
}

TEST(RowsToBatches, BasicUsage) {
  std::vector<std::vector<int>> data = {{1, 2, 4}, {5, 6, 7}};
  auto batches = RowsToBatches(kTestSchema, data, IntConvertor).value_or_die();
  auto table = batches->to_table().value_or_die();

  EXPECT_TRUE(CompareJson(*table, R"([1, 5])", "field_1"));
  EXPECT_TRUE(CompareJson(*table, R"([2, 6])", "field_2"));
  EXPECT_TRUE(CompareJson(*table, R"([4, 7])", "field_3"));
}

TEST(RowsToBatches, ConstRange) {
  const std::vector<std::vector<int>> data = {{1, 2, 4}, {5, 6, 7}};
  auto batches = RowsToBatches(kTestSchema, data, IntConvertor).value_or_die();
  auto table = batches->to_table().value_or_die();

  EXPECT_TRUE(CompareJson(*table, R"([1, 5])", "field_1"));
  EXPECT_TRUE(CompareJson(*table, R"([2, 6])", "field_2"));
  EXPECT_TRUE(CompareJson(*table, R"([4, 7])", "field_3"));
}

TEST(RowsToBatches, StructAccessor) {
  struct TestStruct {
    std::vector<int> values;
  };
  std::vector<TestStruct> data = {TestStruct{{1, 2, 4}}, TestStruct{{5, 6, 7}}};

  auto accessor =
      [](const TestStruct& s) -> turbo::Result<std::reference_wrapper<const std::vector<int>>> {
    return std::cref(s.values);
  };

  auto batches = RowsToBatches(kTestSchema, data, IntConvertor, accessor).value_or_die();

  auto table = batches->to_table().value_or_die();

  EXPECT_TRUE(CompareJson(*table, R"([1, 5])", "field_1"));
  EXPECT_TRUE(CompareJson(*table, R"([2, 6])", "field_2"));
  EXPECT_TRUE(CompareJson(*table, R"([4, 7])", "field_3"));

  // Test accessor that returns by value instead of using `std::reference_wrapper`
  auto accessor_by_value = [](const TestStruct& s) -> turbo::Result<std::set<int>> {
    return std::set<int>(std::begin(s.values), std::end(s.values));
  };
  auto batches_by_value =
      RowsToBatches(kTestSchema, data, IntConvertor, accessor_by_value).value_or_die();

  auto table_by_value = batches_by_value->to_table().value_or_die();

  EXPECT_TRUE(CompareJson(*table_by_value, R"([1, 5])", "field_1"));
  EXPECT_TRUE(CompareJson(*table_by_value, R"([2, 6])", "field_2"));
  EXPECT_TRUE(CompareJson(*table_by_value, R"([4, 7])", "field_3"));
}

TEST(RowsToBatches, Variant) {
  auto VariantConvertor = [](ArrayBuilder& array_builder,
                             const std::variant<int, std::string>& value) {
    if (std::holds_alternative<int>(value))
      return dynamic_cast<Int64Builder&>(array_builder).append(std::get<int>(value));
    else
      return dynamic_cast<nebula::StringBuilder&>(array_builder)
          .append(std::get<std::string>(value));
  };

  const auto test_schema = schema({field("x", int64()), field("y", utf8())});
  std::vector<std::vector<std::variant<int, std::string>>> data = {{1, std::string("2")},
                                                                   {4, std::string("5")}};

  auto batches = RowsToBatches(test_schema, data, VariantConvertor).value_or_die();

  auto table = batches->to_table().value_or_die();

  EXPECT_TRUE(CompareJson(*table, R"([1, 4])", "x"));
  EXPECT_TRUE(CompareJson(*table, R"(["2", "5"])", "y"));
}

}  // namespace nebula::util
